package weixin.app.domain

import java.util.Date
import wechat.base.snd.WCTemplateContent
import weixin.app.WXServiceAppFactoryDemo

import static weixin.app.domain.SendNotification.*

class SendNotification {
	private static String TEMPLATE_NOTIFICATION_MSG_ID = "4HW8NYu6VyRafOPAJdMlQ3FOqnSM0ZQnW21UUJ9Z72Y";// Document
	private static String SERVICE_APP_NOTIFICATION_URL_PREFIX = "http://js.gwaycloud.com/wx/notificationdetail?notificationid="; //jinshan
//jinshan
//{{first.DATA}}
//业务类型：{{keyword1.DATA}}
//业务时间：{{keyword2.DATA}}
//业务内容：{{keyword3.DATA}}
//{{remark.DATA}}
//您好
//业务类型：计划下达
//业务时间：2015-03-12
//业务内容：用于2015年3月“往来款“项目的1950元用款计划已于2015-03-12 16:42:14批复，单号为：PL201503120035。
//如有疑问，请联系业务人员
	def static send(Iterable<String> openids, Date time, String title, String content, String notification_id){
		println('=========>weixin.app.domain.send is called')
		if(openids==null || openids.size == 0){
			println('No wechat user')
			return null
		} 
		WXServiceAppFactoryDemo::serviceApp=>[
		createTemplateNews()=>[
				withTempId(TEMPLATE_NOTIFICATION_MSG_ID)									
				withData(new WCTemplateContent()=>[
					withFirstItem('你好,请关注以下通知')
					withKeyword1Item(title)
					withKeyword2Item(TimeFormatter::dateFormat(time))
					withKeyword3Item(content)
//					withRemarkItem("")
				])
				withUrl(SERVICE_APP_NOTIFICATION_URL_PREFIX+notification_id)
				send2Users(openids)
				openids.forEach[
					println(it)
				]
			]
		]
		
	}
	def static send(String openid, Date time, String title, String content, String notification_id){
		println('=========>weixin.app.domain.send (single openid) is called')
		if (openid == null) {
			println('No wechat user')
			return null	
		}
		send(newArrayList(openid),time, title, content, notification_id)
	}
	def static sendMessage(Iterable<String> openids, String title, String body){
		println('=========>weixin.app.domain.sendMessage is called')
		if(openids==null || openids.size == 0){
			println('No wechat user')
			return null
		} 
		WXServiceAppFactoryDemo::serviceApp=>[
		createTemplateNews()=>[
				withTempId(TEMPLATE_NOTIFICATION_MSG_ID)									
				withData(new WCTemplateContent()=>[
					withFirstItem('你好,请关注以下通知')
					withKeyword1Item(title)
					withKeyword2Item(TimeFormatter::dateFormat(new Date))
					withKeyword3Item(body)
//					withRemarkItem("")
				])
//				withUrl(SERVICE_APP_NOTIFICATION_URL_PREFIX)
				send2Users(openids)
				openids.forEach[
					println(it)
				]
			]
		]		
	}		
}